<?php
declare (strict_types = 1);

namespace app\middleware;

use think\facade\Validate;
use think\exception\ValidateException;
use think\Response;
use think\facade\Db;

class ValidateData
{
    /**
     * 验证规则
     * @var array
     */
    protected $rules = [
        'user.v1.auth/login' => [
            'code' => 'require'
        ],
        'user.v1.auth/savePhone' => [
            'phone' => 'require|mobile'
        ],
        'user.v1.auth/authorization' => [
            'code' => 'require'
        ],
        'user.v1.auth/register' => [
            'phone' => 'require|mobile',
            'password' => 'require|min:6',
            'code' => 'require|length:6'
        ],
        'user.v1.auth/companyAuth' => [
            'companyName' => 'require',
            'businessLicense' => 'require',
            'legalPerson' => 'require',
            'contactPhone' => 'require|mobile'
        ],
        'user.v1.project/save' => [
            'name|项目名称' => 'require|max:40',
            'industry|行业' => 'require',
            'keyword|关键词' => 'require',
            'phone|客户手机号' => 'require',
            'keywords|业务词条' => 'require|array|max:10',
            'id|项目ID' => 'number',
            // 'keywords.*.business|业务词' => 'require',
            // 'keywords.*.question|词条问题' => 'require',
            // 'keywords.*.region|词条地区' => 'require'
        ],
        'user.v1.project/detail'=>[
            'projectId|项目ID' => 'require|number',
            'page|页码' => 'require|number',
            'limit|每页条数' => 'require|number'
        ]
    ];

    /**
     * 处理请求
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 获取所有请求参数
        $data = $request->param();
        $request->data = $data;
        $controller = $request->controller();
        $action = $request->action();
        $key = strtolower($controller . '/' . $action);
        if (isset($this->rules[$key])) {
            try {
                validate($this->rules[$key])->check($data);
            } catch (ValidateException $e) {
                return result(FAIL, $e->getError());
            }
        }

        // 设置自动时间戳
        Db::listen(function($sql, $time, $explain){
            // 获取当前时间
            $now = date('Y-m-d H:i:s');
            
            // 如果是插入操作
            if (stripos($sql, 'INSERT INTO') !== false) {
                // 获取表名
                preg_match('/INSERT INTO `?(\w+)`?/i', $sql, $matches);
                if (isset($matches[1])) {
                    // 如果有createTime字段，添加createTime
                    $sql .= ",createTime = `{$now}`";
                }
            }
            
            // 如果是更新操作
            if (stripos($sql, 'UPDATE') !== false) {
                // 获取表名
                preg_match('/UPDATE `?(\w+)`?/i', $sql, $matches);
                if (isset($matches[1])) {
                   $sql .= ",updateTime = `{$now}`";
                }
            }
        });

        return $next($request);
    }
} 